<?php
/**
 * Created by PhpStorm.
 * User: Tioncico
 * Date: 2018/10/18 0018
 * Time: 9:43
 */

namespace App\Process;

use App\Logic\Account\StatisticsService;
use App\Logic\Member\MemberProfitStatisticsService;
use App\Logic\Member\MemberService;
use App\Logic\Order\OrderService;
use App\Logic\Order\OrderTaskStatisticsService;
use App\Logic\Purchase\PurchaseService;

class Task
{

    /**自动统计用户订单数和收益*/
    static function memberOrderCount(): bool
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('memberOrderCount');
        MemberService::getInstance()->taskCountUserData();
        return true;
    }

    /**按日期统计收益*/
    static function memberProfitCount(): bool
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('memberProfitCount');
        MemberProfitStatisticsService::getInstance()->statistics();
        return true;
    }

    /**统计每日订单任务汇总*/
    static function orderTaskStatistics(): bool
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('orderTaskStatistics');
        return true;
        OrderTaskStatisticsService::getInstance()->statistics();
    }

    /**每日自动结算*/
    static function orderAutoSettle(): bool
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('orderAutoSettle');
        OrderService::getInstance()->taskOrderAutoSettle();
        return true;
    }

    /**统计所有帐户和订单*/
    static function statisticsAll(): bool
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('statisticsAll');
        StatisticsService::getInstance()->jobStatisticsAll();
        return true;
    }

    /**
     * 自动更新订单状态 SHIPPED:已发货;
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    static function orderStateShipped(): bool
    {
        return PurchaseService::getInstance()->orderStateShipped();
    }
    /**
     * 自动更新订单状态 RECEIPT:确认收货;
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    static function orderStateReceipt(): bool
    {
        return PurchaseService::getInstance()->orderStateReceipt();
    }

    private $count = 0;

    public function run() {
        // 任务执行的代码，每次执行时自增计数器
//        var_dump($this->count);
        $this->count++;

        // 如果计数器大于等于 10，或者返回值是 true，就停止执行
        if ($this->count >= 10 || $this->doSomething()) {
            return true;
        }

        // 返回 false 继续执行任务
        return false;
    }

    private function doSomething() {
        // 执行任务的代码
        // 如果任务已经完成，返回 true
        return false;
    }
}
